package 数据结构.tree.threadedbinarytree;

public class ThreadedBinaryTreeDemo {
    public static void main(String[] args) {
        HeroNode root = new HeroNode(1, "jack");
        HeroNode node2 = new HeroNode(3, "tony");
        HeroNode node3 = new HeroNode(6, "tom");
        HeroNode node4 = new HeroNode(8, "mary");
        HeroNode node5 = new HeroNode(10, "joe");
        HeroNode node6 = new HeroNode(14, "sam");
        root.setLeft(node2);
        root.setRight(node3);
        node2.setLeft(node4);
        node2.setRight(node5);
        node3.setLeft(node6);
        //线索化
        ThreadBinaryTree threadBinaryTree = new ThreadBinaryTree();
        threadBinaryTree.setRoot(root);
        threadBinaryTree.threadNodes();
        //测试 10号节点
        HeroNode heroNode = node5.getLeft();
        System.out.println(heroNode);
        System.out.println("10号节点的前驱结点" + heroNode);

        System.out.println("===========");
        System.out.println("使用线索化方式遍历二叉树");
        threadBinaryTree.threadedList();
    }
}

//实现了线索化功能的二叉树
class ThreadBinaryTree {
    private HeroNode root;

    /**
     * 遍历线索化二叉树
     */
    public void threadedList() {
        HeroNode node = root;
        while (node != null) {
            //第一个type==1节点就是8
            while (node.getLeftType() == 0) {
                node = node.getLeft();
            }
            //找到这个节点
            System.out.println(node);
            while (node.getRightType() == 1) {
                //获取当前节点的后继节点
                node = node.getRight();
                System.out.println(node);
            }
            //替换这个节点
            node = node.getRight();
        }
    }

    //为了实现线索化,需要一个指向当前节点的前驱节点的指针
    //时间上保留的就是前一个节点.可能用于实现前驱节点
    private HeroNode pre = null;

    public void setRoot(HeroNode root) {
        this.root = root;
    }

    public void threadNodes() {
        this.threadNodes(root);
    }

    //执行中序线索花的方法
    public void threadNodes(HeroNode node) {
        //node 是当前需要线索花的节点
        if (node == null) {
            return;
        }
        //(一)先处理左子树

        threadNodes(node.getLeft());
        //(二)线索化当前节点
        if (node.getLeft() == null) {
            //当前节点的左指针指向前驱结点
            node.setLeft(pre);
            node.setLeftType(1);
        }

        if (pre != null && pre.getRight() == null) {
            pre.setRight(node);
            pre.setRightType(1);
        }
        pre = node;
        //(三)线索化后继节点
        threadNodes(node.getRight());
    }

    //前序遍历
    public void preOrder() {
        if (this.root != null) {
            this.root.preOredr();
        } else {
            System.out.println("二叉树为空,无法遍历");
        }
    }

    public void infixOrder() {
        if (this.root != null) {
            this.root.infixOrder();
        } else {
            System.out.println("二叉树为空,无法遍历");
        }
    }

    public void afterOrder() {
        if (this.root != null) {
            this.root.afterOrder();
        } else {
            System.out.println("二叉树为空,无法遍历");
        }
    }

    //查找方法:前序列
    public HeroNode preOrdersSearch(int no) {
        if (root != null) {
            return root.preOrdersearch(no);
        } else {
            return null;
        }
    }

    public HeroNode infixOrdersSearch(int no) {
        if (root != null) {
            return root.infixOrdersSearch(no);
        } else {
            return null;
        }
    }

    public HeroNode afterOrdersSearch(int no) {
        if (root != null) {
            return root.afterOrdersSearch(no);
        } else {
            return null;
        }
    }

    public void delete(int no) {
        if (root != null) {
            //如果只有一个root节点,这里立即判断root是不是要删除的元素
            if (root.getNo() == no) {
                root = null;
            } else {
                root.deletNode(no);
            }
        }
    }
}


//创建一个Hero
class HeroNode {
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    private int leftType;//约定 ==0表示的是左子树,如果是1指向前驱节点
    private int rightType;//约定 ==0表示的是右子树,如果是1指向后继节点

    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getLeftType() {
        return leftType;
    }

    public void setLeftType(int leftType) {
        this.leftType = leftType;
    }

    public int getRightType() {
        return rightType;
    }

    public void setRightType(int rightType) {
        this.rightType = rightType;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    //编写遍历的方法
    public void preOredr() {
        System.out.println(this);
        //递归向左子树前序遍历
        if (this.left != null) {
            this.left.preOredr();
        }
        if (this.right != null) {
            this.right.preOredr();
        }
    }

    //中序遍历
    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }

    //后序遍历
    public void afterOrder() {
        if (this.left != null) {
            this.left.afterOrder();
        }
        if (this.right != null) {
            this.right.afterOrder();
        }
        System.out.println(this);
    }

    //前序遍历查找
    public HeroNode preOrdersearch(int no) {
        System.out.println("一次前序产找");
        //先比较当前节点
        if (this.no == no) {
            return this;
        }
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.preOrdersearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        if (this.right != null) {
            resNode = this.right.preOrdersearch(no);
        }
        return resNode;
    }

    //中序遍历查找
    public HeroNode infixOrdersSearch(int no) {

        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.infixOrdersSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        System.out.println("中序遍历");
        if (this.no == no) {
            return this;
        }
        if (this.right != null) {
            resNode = this.right.infixOrdersSearch(no);
        }
        return resNode;
    }

    //后序遍历查找
    public HeroNode afterOrdersSearch(int no) {
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.afterOrdersSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        if (this.right != null) {
            resNode = this.right.afterOrdersSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        System.out.println("后序查找");
        if (this.no == no) {
            return this;
        }
        return resNode;
    }

    //删除节点
    public void deletNode(int no) {
        if (this.left != null && this.left.no == no) {
            this.left = null;
            return;
        }
        if (this.right != null && this.right.no == no) {
            this.right = null;
            return;
        }
        if (this.left != null) {
            this.left.deletNode(no);

        }
        if (this.right != null) {
            this.right.deletNode(no);
        }
    }
}